<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Intervention\Image\ImageManagerStatic as Image;
use App\Http\Requests\StorePersonRequest;
use App\Http\Requests\UpdatePersonReuqest;
use App\Repositorys\PersonRepository;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;


class PersonController extends Controller
{

    protected $person;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(PersonRepository $person, Request $request)
    {
        $this->middleware('auth');
        $this->person = $person;

        $this->middleware(function ($request, $next) {
            $this->user = $request->user();

            return $next($request);
        });
    }


    /**
     * 人员列表页
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request, Log $log)
    {
        $filter = []; // 查询参数

        $per_page = config('system.person.number'); // 每页记录数量

        $years = array_reverse($this->person->years()); // 所有年份

        $persons = $this->person->pageWithRequest($request, $filter, $per_page, null); // 分页


        return view('person.index', compact('persons', 'filter', 'per_page', 'years'));
    }

    /**
     * 人员回收站页面
     *
     * @param Request $request
     * @return mixed
     */
    public function recycleIndex(Request $request)
    {
        $filter = []; // 查询参数

        $per_page = config('system.person.number'); // 每页记录数量

        $years = array_reverse($this->person->years()); // 所有年份

        $persons = $this->person->pageWithRequest($request, $filter, $per_page, true); // 分页

        return view('person.recycle_index', compact('persons', 'filter', 'per_page', 'years'));
    }

    /**
     * 添加人员页面
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed|\think\response\View
     */
    public function create()
    {
        return view('person.create');
    }

    /**
     * 添加人员信息
     *
     * @param StorePersonRequest $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function store(StorePersonRequest $request)
    {
        $this->person->saveWithRequest($request);

        return redirect('person')->with('success', '添加人员信息成功');
    }

    /**
     * 编辑人员页面
     *
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response|\think\Response
     */
    public function edit($id)
    {
        $person = $this->person->getEditData($id);

        if (! $person) {
            return response(view('errors.404'), 404);
        }

        return view('person.edit', ['person' => $person]);
    }

    /**
     * 更新人员到数据库
     *
     * @param Request $request
     * @param $id
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function update(UpdatePersonReuqest $request)
    {
        $person = $this->person->getById($request->input('id'));
        if (! $person) {
            return response(view('errors.404'), 404);
        }

        $this->person->updateWithRequest($request->all(), $person);

        return redirect()->back()->with('success', '编辑人员信息成功');
    }

    /**
     * 从回收站删除人员
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(request $request)
    {
        $this->person->destroy($request->input('id'));

        return redirect()->back();
    }

    /**
     * 清空回收站
     * @return $this
     */
//    public function destroyAll()
//    {
//
//        $data = DB::table('persons')->where('is_deleted', '=', '1')->delete();
//
//        return redirect()->back()->withInput()->withErrors('清空回收作站成功！');
//    }

    /**
     * 添加至回收站
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function recycle(Request $request)
    {
        $this->person->recycle($request->input('id'));

        return redirect()->back();
    }


    /**
     * 从回收站还原
     *
     * @param Request $request
     * @return $this
     */
    public function recover(Request $request)
    {

        $this->person->recover($request);

        return redirect()->back()->withInput()->withErrors('已成功还原！');
    }

    /**
     * 单个人员信息页面
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        $person = $this->person->getById($id);

        return view('person.show', ['person' => $person]);
    }

    /**
     * 切换是否本市
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function isCity(Request $request)
    {

        $this->person->isCity($request);

        return redirect()->back();
    }

    /**
     * 测试页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed|\think\response\View
     */
    public function test()
    {
        return view('person/test');
    }

    /**
     * 导出EXCEL表格
     *
     * @param Request $request
     */
    public function excel(Request $request)
    {
        $this->person->excelWithRequest($request);
    }
}
